package com.sugon.api;


import com.sugon.annotation.AuthCheck;
import com.sugon.annotation.LoginUser;
import com.sugon.entity.ApiResult;
import com.sugon.entity.DevMaintenRepairEntity;
import com.sugon.entity.SysUserEntity;
import com.sugon.service.DevMaintenRepairService;
import com.sugon.util.ApiBaseAction;
import com.sugon.util.ApiPageUtils;
import com.sugon.utils.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "设备维修记录")
@RestController
@RequestMapping("/api/devmaintenrepair")
public class ApiDevMaintenRepairController extends ApiBaseAction {

    @Autowired
    private DevMaintenRepairService devMaintenRepairService;

    /**
     * 设备维修记录查询
     */
    @ApiOperation(value = "设备维修记录查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "devId", value = "设备ID", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "limit", value = "每页记录数", paramType = "query", dataType = "Integer", required = true),
    })
    @RequestMapping(value = "getDevMaintenRepairList", method = RequestMethod.POST)
    public ApiResult<ApiPageUtils> getDevMaintenWorkList(@ApiIgnore @RequestParam Map<String, Object> params) {
        ApiResult<ApiPageUtils> apiResult = new ApiResult<>();
        Query query = new Query(params);
        List<DevMaintenRepairEntity> devMaintenRepairList = devMaintenRepairService.queryList(query);
        int total = devMaintenRepairService.queryTotal(query);
        return apiResult.success(new ApiPageUtils(devMaintenRepairList, total, query.getLimit(), query.getPage()));
    }

    /**
     * 设备维修记录查询详情情接口
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "设备维修记录查询详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "维修记录Id", paramType = "query", dataType = "String", required = true)
    })
    @RequestMapping(value = "getDetailById", method = RequestMethod.POST)
    public Object getDetailById(@RequestParam(value = "id") String id) {
        DevMaintenRepairEntity devMaintenRepairEntity = devMaintenRepairService.queryObject(id);
        return toResponsSuccess(devMaintenRepairEntity);
    }

    /**
     * 填写设备维修记录接口
     */
    @AuthCheck
    @ApiOperation(value = "添加设备维修记录API")
    @RequestMapping(value = "saveDevMaintainWork", method = RequestMethod.POST)
    public Object saveDevMaintainWork(
            @ApiIgnore @LoginUser SysUserEntity loginUser,
            @RequestBody DevMaintenRepairEntity devMaintenRepairEntity
    ) {
        int rows = devMaintenRepairService.save(devMaintenRepairEntity);
        if (rows > 0) {
            Map<String, Object> data = new HashMap<>();
            data.put("maintainPlan", devMaintenRepairEntity);
            data.put("userId", loginUser.getUserId());
            return toResponsSuccess(data);
        } else {
            return toResponsFail("添加维修记录失败");
        }
    }

}
